package com.cn.wanxi.util;

import java.io.*;
import java.util.List;

public class IO {

    private static File file;

    /**
     * 创建文件
     *
     * @param path     地址
     * @param temp     创建文件夹
     * @param fileName 文件名称
     * @return
     */
    public static void createNote(String path, String temp, String fileName) {
        File directory = new File(path, temp);
//        if(!directory.exists()){
        directory.mkdirs();
//        }
        File tempFile = new File(directory, fileName);
//        if(!file.exists()){
        try {
            tempFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        }
        file = tempFile;
    }

    /**
     * @param list
     */
    public static void writeNote(List<String> list, String fileName) {
        createNote(PathEnum.PATH.getValue(), PathEnum.TEMP.getValue(), fileName);
        try {
            OutputStream outputStream = new FileOutputStream(file);
            for (String value : list
            ) {
                outputStream.write(value.getBytes());
                outputStream.write(PathEnum.SPLIT.getValue().getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return
     */
    public static String readNote() {
//        String 和StringBuilder和StringBuffer的区别？
//        String str = "a";
//        String str1 = "a";
//        System.out.println(str == str1);
//        String str2 = new String("a");
//        System.out.println(str == str2);
//        String str3 = new String("a");
//        System.out.println(str2 == str3);

        StringBuilder result = new StringBuilder();
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int read = 0;
//            while ((read = inputStream.read()) != -1) {
//                System.out.print((char) read);
//            }
//            读取中文的时候需要用到字符流，不能用字节流

            Reader reader = new FileReader(file);
            int read = 0;
//            byte b=1;
//            b+=1;
//            b=b+1;

            while ((read = reader.read()) != -1) {
                result.append((char) read);
            }
//            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

}
